package com.fire.control.system.module.company.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.control.system.dto.SelectCompanyDTO;
import com.fire.control.system.entity.Company;
import com.fire.control.system.module.company.dao.CompanyMapper;
import com.fire.control.system.module.company.service.CompanyService;

@Service
public class CompanyServiceImpl implements CompanyService{
	
	@Autowired private CompanyMapper companyMapper;
	
	public List<String> getChildCompanyIdList(String companyId){
		List<Company> companyList = companyMapper.getAllCompanys();
		List<String> companyIds = new ArrayList<String>();
		companyIds.add(companyId);
		getChildCompanyIdList(companyIds,companyList,companyId);
		return companyIds;
	}
	
	private void getChildCompanyIdList(List<String> companyIds,List<Company> companyList,String pid){
		for (Company company : companyList) {
			String id = company.getId();
			String parentId = company.getParentId();
			if(pid.equals(parentId)){
				companyIds.add(id);
				getChildCompanyIdList(companyIds,companyList,id);
			}
		}
	}
	
	/**
	 * @author: xiechengguang 
	 * @Title: getSelectCompanyList   
	 * @Description: TODO  
	 * @param:        
	 * @return: List<SelectCompanyDTO>      
	 * @throws
	 */
	public List<SelectCompanyDTO> getSelectCompanyList(String companyId){
		List<Company> companyList = companyMapper.getAllCompanys();
		List<SelectCompanyDTO> companyDtos = new ArrayList<SelectCompanyDTO>();
		SelectCompanyDTO dto = companyMapper.getCompanyDto(companyId);
		companyDtos.add(dto);
		getSelectCompanyList(companyDtos,companyList,companyId);
		return companyDtos;
	}
	
	/**
	 * @author: xiechengguang 
	 * @Title: getSelectCompanyList   
	 * @Description: TODO  
	 * @param:        
	 * @return: void      
	 * @throws
	 */
	private void getSelectCompanyList(List<SelectCompanyDTO> companyDtos,List<Company> companyList,String pid){
		for (Company company : companyList) {
			String id = company.getId();
			String parentId = company.getParentId();
			if(pid.equals(parentId)){
				SelectCompanyDTO dto = companyMapper.getCompanyDto(id);
				companyDtos.add(dto);
				getSelectCompanyList(companyDtos,companyList,id);
			}
		}
	}
	
}
